package com.ysy.tz.core.selfcomp;

import java.io.ByteArrayInputStream;
import java.util.Iterator;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class CustomerAlert extends Alert {
    public CustomerAlert(Alert.AlertType alertType, String contentText, ButtonType... buttons) {
        super(alertType, contentText, buttons);
        this.getDialogPane().getChildren().remove(1);
        GridPane grid = new GridPane();
        grid.setStyle("-fx-padding: 0.833em; /* 10px */\n    -fx-background-color: -fx-box-border, linear-gradient(-fx-background, derive(-fx-background, 30%));\n    -fx-background-insets: 0, 0 0 1 0;");
        grid.setHgap(10.0);
        ColumnConstraints graphicColumn = new ColumnConstraints();
        graphicColumn.setFillWidth(false);
        graphicColumn.setHgrow(Priority.NEVER);
        ColumnConstraints textColumn = new ColumnConstraints();
        textColumn.setFillWidth(true);
        textColumn.setHgrow(Priority.ALWAYS);
        grid.getColumnConstraints().setAll(new ColumnConstraints[]{graphicColumn, textColumn});
        grid.setPadding(new Insets(5.0, 0.0, 5.0, 15.0));
        Label image = new Label();
        switch (alertType) {
            case CONFIRMATION:
                image.setStyle("-fx-graphic: url('/com/sun/javafx/scene/control/skin/modena/dialog-confirm.png');");
                break;
            case WARNING:
                image.setStyle("-fx-graphic: url('/com/sun/javafx/scene/control/skin/modena/dialog-warning.png');");
                break;
            case INFORMATION:
                image.setStyle("-fx-graphic: url('/com/sun/javafx/scene/control/skin/modena/dialog-information.png');");
                break;
            case ERROR:
                image.setStyle("-fx-graphic: url('/com/sun/javafx/scene/control/skin/modena/dialog-error.png');");
        }

        grid.add(image, 0, 0);
        if (contentText.startsWith("<TextFlow>")) {
            SAXReader saxReader = new SAXReader();

            try {
                Document document = saxReader.read(new ByteArrayInputStream(contentText.getBytes("UTF-8")));
                TextFlow textFlow = new TextFlow();

                Text text;
                for(Iterator var11 = document.getRootElement().elements().iterator(); var11.hasNext(); textFlow.getChildren().add(text)) {
                    Element element = (Element)var11.next();
                    text = new Text();
                    text.setText(element.getText());
                    String style = element.attributeValue("style");
                    if (StringUtils.isNotBlank(style)) {
                        text.setStyle(style);
                    }
                }

                grid.add(textFlow, 1, 0);
            } catch (Exception var15) {
                var15.printStackTrace();
            }
        } else {
            Label headerLabel = new Label(contentText);
            headerLabel.setWrapText(true);
            headerLabel.setAlignment(Pos.CENTER_LEFT);
            headerLabel.setMaxWidth(Double.MAX_VALUE);
            headerLabel.setMaxHeight(Double.MAX_VALUE);
            grid.add(headerLabel, 1, 0);
        }

        this.getDialogPane().setHeader(grid);
    }
}
